package net.digitalartwork.ui{
	import flash.display.*;

	import flash.events.*;
	import flash.net.URLRequest;
	import flash.utils.*;
	import flash.text.*;
	
	import net.digitalartwork.util.*;
	import net.digitalartwork.players.*;
	import net.digitalartwork.ui.*;
	import net.digitalartwork.text.*;
	import gs.TweenFilterLite;
	import gs.OverwriteManager;
	import gs.easing.*;
	
	import net.digitalartwork.ui.ViewModeThumb;
	
	public class ImageMakerLite extends Sprite{
		//--------------------------------------------------------------------------
		// ASSETS
		//--------------------------------------------------------------------------
		public var imageContainer:Sprite;
		public var imageHolder:Bitmap;
		public var imageOutline:Sprite;
		private var loadingWindow:CircleProgress;
		private var viewModeContainer:ViewModeThumb;
		//--------------------------------------------------------------------------
		// PUBLIC VARIABLES
		//--------------------------------------------------------------------------
		public var _date:Date;
		public var _edit:Number;
		public var _color:String;
		public var _rating:String;
		
		public var sort_object:Object;
		//--------------------------------------------------------------------------
		// VARIABLES
		//--------------------------------------------------------------------------
		private var _var:String;
		private var _number:Number;
		private var _select:Boolean = false;
		private var _y:Number;
		private var _x:Number;
		private var _width:Number = 100;
		private var _height:Number = 100;
		//--------------------------------------------------------------------------
		private var monthLabels:Array = new Array("Jan.","Feb.","Mar.","Apr.","May","Jun.", "Jul.","Aug.","Sept.","Oct.","Nov.","Dec.");
		//--------------------------------------------------------------------------
		private var finalImage:Boolean = false;
		private var firstLoadCheck:Boolean = true;
		private var enableFinalImage:Boolean = false;
		//--------------------------------------------------------------------------
		private var imageLoader:Loader;
		private var imageURLReq:URLRequest;
		private var imageXML:XML;
		//--------------------------------------------------------------------------
		// DISPATCH
		//--------------------------------------------------------------------------
		public var IMAGE_LOADING:String = "imageloading";
		public var IMAGE_LOADED:String = "imageloaded";
		public var IMAGE_ROLLOVER:String = "imagerollover";
		public var IMAGE_ROLLOUT:String = "imagerollout";
		public var IMAGE_DOWN:String = "imagedown";
		//--------------------------------------------------------------------------
		// DISPATCH
		//--------------------------------------------------------------------------
		
		
		
		public function ImageMakerLite(img:Bitmap, so:Object, w:Number, h:Number, nm:Number=-1, scaleMe:Boolean=false, fi:String="", ol:Boolean=false) {
			
			sort_object = so;
			
			_date = sort_object._date;
			_edit = sort_object._edit;
			_color = sort_object._color;
			_rating = sort_object._rating;
			
			
			
			viewModeContainer = new ViewModeThumb();
			
			
			if (ol) {
				imageOutline = new Sprite();
				imageOutline.graphics.beginFill(0x999999, 0);
				imageOutline.graphics.lineStyle(2, 0x222222, 1, true);
				imageOutline.graphics.drawRect(0, 0, w+2, h+2);
				addChild(imageOutline);
				imageOutline.x = -1;
				imageOutline.y = -1;
			}
			
			
			if (scaleMe){
				_width = w/10;
				_height = h/10;
			} else {
				_width = w;
				_height = h;
			}
			
			if (fi == ""){
				enableFinalImage = false;
			} else {
				_var = fi;
				enableFinalImage = true;
			}
			if (nm == -1) {
				
			} else {
				_number = nm;
			}
			
			imageContainer = new Sprite();
			addChild(imageContainer);
			
			imageHolder = img;
			
			imageContainer.addChild(imageHolder);
			
			imageContainer.alpha=0;
			TweenFilterLite.to(imageContainer, .3, {alpha:1, ease:Linear.easeOut});
			
			imageHolder.width = _width;
			imageHolder.height = _height;
			
			imageContainer.addEventListener(MouseEvent.ROLL_OVER, imageOverHandeler); 
			imageContainer.addEventListener(MouseEvent.ROLL_OUT, imageOutHandeler);
			imageContainer.addEventListener(MouseEvent.CLICK, imageDownHandeler);
			
			dispatchEvent(new Event(IMAGE_LOADED));
			loadFinalImage();
		}
		private function imageOverHandeler(event:Event):void{
			dispatchEvent(new Event(IMAGE_ROLLOVER));
		}
		private function imageOutHandeler(event:Event):void{
			dispatchEvent(new Event(IMAGE_ROLLOUT));

		}
		private function imageDownHandeler(event:Event):void{
			dispatchEvent(new Event(IMAGE_DOWN));
		}

		
		private function loadFinalImage():void {
			if (enableFinalImage){
				if (!finalImage) {
					loadingWindow = new CircleProgress(_width, _height);
					imageContainer.addChild(loadingWindow);
					loadingWindow.showBar();
					imageLoader = new Loader();
					imageURLReq = new URLRequest(_var);
					imageLoader.load(imageURLReq);
					imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoadedHandeler);
					imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageProgressHandeler);
					
				}
			}
		}
		
		public function viewMode(mode:String="none"):void {
			
			if (mode == "none") {
				
				if (imageContainer.contains(viewModeContainer)) {
					imageContainer.removeChild(viewModeContainer);
				}
			}
			
			if (mode == "date") {
				//var timeT:String = hourMinuteTimeFormat(_date.getHours(), _date.getMinutes());
				var timeT:String = doubleDigitFormat(_date.getHours()) + ":" + doubleDigitFormat(_date.getMinutes())
				//var monthdayT:String = monthLabels[_date.getMonth()] + " " + _date.getDate();
				var monthdayT:String = _date.getMonth() + "/" + _date.getDate();
				var yearT:String = _date.getFullYear().toString();


				viewModeContainer.viewModeDate(timeT, monthdayT, yearT);
				viewModeContainer.alpha = 0;
				imageContainer.addChild(viewModeContainer);
				
				viewModeContainer.mouseEnabled = false;
				viewModeContainer.mouseChildren = false;
				
				TweenFilterLite.to(viewModeContainer, .2, {dropShadowFilter:{color:0x000000, alpha:1, blurX:10, blurY:10, strength:1, distance:0}, alpha:1, ease:Linear.easeNone});
			}
			if (mode == "color") {
				trace(_color)
				viewModeContainer.viewModeColor(_color);
				viewModeContainer.alpha = 0;
				imageContainer.addChild(viewModeContainer);
				
				viewModeContainer.mouseEnabled = false;
				viewModeContainer.mouseChildren = false;
				
				TweenFilterLite.to(viewModeContainer, .2, {dropShadowFilter:{color:0x000000, alpha:1, blurX:10, blurY:10, strength:1, distance:0}, alpha:1, ease:Linear.easeNone});
			}
			
			
		}
		
		private function imageProgressHandeler(event:ProgressEvent):void{
			
			var progressPct:Number = event.bytesLoaded/event.bytesTotal;
			loadingWindow.updateProgress(progressPct);
			dispatchEvent(new Event(IMAGE_LOADING));
		}
		
		private function imageLoadedHandeler(event:Event):void{
			finalImage = true;
			
			var imageFinal:Sprite = new Sprite();
			imageFinal.addChild(imageLoader.content);
			imageFinal.alpha = 0;
			imageContainer.addChild(imageFinal);
			
			TweenFilterLite.to(imageFinal, .5, {alpha:1, onComplete:finishLoad});
			TweenFilterLite.to(loadingWindow, .5, {alpha:0, onComplete:finishLoad});
			
			function finishLoad():void {
				
				imageLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, imageLoadedHandeler);
				imageLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, imageProgressHandeler);
				
				if (imageContainer.contains(imageHolder)) {
					imageContainer.removeChild(imageHolder);
				}
				
				loadingWindow.hideBar();
			}

		}
		
		private function hourMinuteTimeFormat(hrs:Number, mins:Number):String {
			var modifier:String = "PM";
			var minLabel:String = doubleDigitFormat(mins);
			
			if(hrs > 12) {
				hrs = hrs-12;
			} else if(hrs == 0) {
				modifier = "AM";
				hrs = 12;
			} else if(hrs < 12) {
				modifier = "AM";
			}

			return (doubleDigitFormat(hrs) + ":" + minLabel + " " + modifier);
		}
		
		private function doubleDigitFormat(num:Number):String {
			if(num < 10) {
		        return ("0" + num);
		    }
		    return num.toString();
		}
		
		
		public function removeImage():void{
			
		}
		public function makeBW(theAlpha:Number=1):void {
			TweenFilterLite.to(imageHolder, .2, {colorMatrixFilter:{amount:1, saturation:0}, alpha:theAlpha});
		}
		public function makeColor(theAlpha:Number=1):void {
			TweenFilterLite.to(imageHolder, .2, {colorMatrixFilter:{amount:1, saturation:1}, alpha:theAlpha});
		}
		public function makeColorize(theAlpha:Number=1, theColor:Number=0x336699):void {
			TweenFilterLite.to(imageHolder, .2, {alpha:theAlpha, colorMatrixFilter:{colorize:theColor, amount:1}});
		}
		public function makeAlpha(theAlpha:Number=1):void {
			TweenFilterLite.to(imageHolder, .2, {alpha:theAlpha});
		}
		public function getVar():String {
			return _var;
		}
		public function setVar(theVar:String):void {
			_var = theVar;
		}
		public function getNumVar():Number {
			return _number;
		}
		public function setNumVar(theVar:Number):void {
			_number = theVar;
		}
		
		public function getFullLoaded():Boolean {
			return true;
		}
		public function setFullLoaded(theVar:Boolean):void {
			//imageFullLoaded = theVar;
		}
		
		public function getSelectStatus():Boolean {
			return _select;
		}
		public function setSelectStatus(theVar:Boolean):void {
			_select = theVar;
		}
		public function getImageXMLString(theVar:String):String {
			return imageXML.attribute(theVar);
		}
		public function getImageXML(theVar:String):Number {
			return imageXML.attribute(theVar);
		}
		public function setImageXML(theVar:XML):void {
			imageXML = theVar;
		}
		public function getXML():XML {
			return imageXML;
		}
		public function setXY(theX:Number, theY:Number):void {
			_x = theX;
			_y = theY;
		}
		public function getX():Number {
			return _x;
		}
		public function getY():Number {
			return _y;
		}
	}
}